YOLO Env Config

Conda 环境配置

安装conda(运行环境隔离)

[Verifying - USTC Mirrors](https://mirrors.ustc.edu.cn/anaconda/miniconda/Miniconda3-py310_24.9.2-0-Windows-x86_64.exe)

输入 conda create -n yolov8 python=3.10 新建环境

(base) C:\Users\Chi>conda create -n yolov8 python=3.10
Channels:
 - defaults
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: C:\Users\Chi\miniconda3\envs\yolov8

  added / updated specs:
    - python=3.10


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2025.2.25  |       haa95532_0         130 KB
    openssl-3.0.16             |       h3f729d1_0         7.8 MB
    pip-25.0                   |  py310haa95532_0         2.5 MB
    python-3.10.16             |       h4607a30_1        16.3 MB
    setuptools-75.8.0          |  py310haa95532_0         1.7 MB
    tzdata-2025a               |       h04d1e81_0         117 KB
    vc-14.42                   |       haa95532_4          11 KB
    vs2015_runtime-14.42.34433 |       he0abc0d_4         1.2 MB
    wheel-0.45.1               |  py310haa95532_0         145 KB
    xz-5.6.4                   |       h4754444_1         280 KB
    ------------------------------------------------------------
                                           Total:        30.1 MB

The following NEW packages will be INSTALLED:

  bzip2              pkgs/main/win-64::bzip2-1.0.8-h2bbff1b_6
  ca-certificates    pkgs/main/win-64::ca-certificates-2025.2.25-haa95532_0
  libffi             pkgs/main/win-64::libffi-3.4.4-hd77b12b_1
  openssl            pkgs/main/win-64::openssl-3.0.16-h3f729d1_0
  pip                pkgs/main/win-64::pip-25.0-py310haa95532_0
  python             pkgs/main/win-64::python-3.10.16-h4607a30_1
  setuptools         pkgs/main/win-64::setuptools-75.8.0-py310haa95532_0
  sqlite             pkgs/main/win-64::sqlite-3.45.3-h2bbff1b_0
  tk                 pkgs/main/win-64::tk-8.6.14-h0416ee5_0
  tzdata             pkgs/main/noarch::tzdata-2025a-h04d1e81_0
  vc                 pkgs/main/win-64::vc-14.42-haa95532_4
  vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.42.34433-he0abc0d_4
  wheel              pkgs/main/win-64::wheel-0.45.1-py310haa95532_0
  xz                 pkgs/main/win-64::xz-5.6.4-h4754444_1
  zlib               pkgs/main/win-64::zlib-1.2.13-h8cc25b3_1


Proceed ([y]/n)?

输入 y

Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate yolov8
#
# To deactivate an active environment, use
#
#     $ conda deactivate

conda env list 查看现有环境

(base) C:\Users\Chi>conda env list
# conda environments:
#
base                  *  C:\Users\Chi\miniconda3
yolov8                   C:\Users\Chi\miniconda3\envs\yolov8

然后用 conda activate yolov8 激活环境,我们现在在 yolov8 的环境下

(base) C:\Users\Chi>conda activate yolov8

(yolov8) C:\Users\Chi>conda env list
# conda environments:
#
base                     C:\Users\Chi\miniconda3
yolov8                *  C:\Users\Chi\miniconda3\envs\yolov8

配置清华源

(yolov8) C:\Users\Chi>pip config set global.index-url https://mirror.tuna.tsinghua.edu.cn/pypi/web/simple
Writing to C:\Users\Chi\AppData\Roaming\pip\pip.ini
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes

在 cmd 中激活环境:

conda init cmd.exe

关闭再打开

conda activate yolov8

pytorch 配置

查看显卡支持的 CUDA 版本:12.3

cuda_version.png

30系显卡需要安装 CUDA 111 以上的版本

conda install pytorch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1  pytorch-cuda=11.8 -c pytorch -c nvidia

测试一切是否正确:

(yolov8) C:\Users\Chi>python
Python 3.10.16 | packaged by Anaconda, Inc. | (main, Dec 11 2024, 16:19:12) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.ones(3).cuda()
tensor([1., 1., 1.], device='cuda:0')
>>> exit()

下载YOLOv8

    目录: D:\gesture_recog\ultralytics-8.1.0


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2025/3/16     22:09                .github
d-----         2025/3/16     22:09                docker
d-----         2025/3/16     22:09                docs
d-----         2025/3/16     22:09                examples
d-----         2025/3/16     22:09                tests
d-----         2025/3/16     22:09                ultralytics
-a----         2025/3/16     22:09           2295 .gitignore
-a----         2025/3/16     22:09           2408 .pre-commit-config.yaml
-a----         2025/3/16     22:09            612 CITATION.cff
-a----         2025/3/16     22:09           5585 CONTRIBUTING.md
-a----         2025/3/16     22:09          34523 LICENSE
-a----         2025/3/16     22:09           6728 pyproject.toml
-a----         2025/3/16     22:09          35898 README.md
-a----         2025/3/16     22:09          34774 README.zh-CN.md


(yolov8) PS D:\gesture_recog\ultralytics-8.1.0> pip install -e .

下载

pip install jupyterlab tensorboard

环境配置错误

pip uninstall pillow # 删除原先的 9.3.0 版本
pip install pillow==9.3.0 # 重新下载一遍,好了

好了

(yolov8) PS D:\gesture_recog\ultralytics-8.1.0> yolo
C:\Users\Chi\miniconda3\envs\yolov8\lib\site-packages\torchvision\io\image.py:14: UserWarning: Failed to load image Python extension: '[WinError 127] 找不到指定的程序。'If you don't plan on using image functionality from `torchvision.io`, you can ignore this warning. Otherwise, there might be something wrong with your environment. Did you have `libjpeg` or `libpng` installed before building `torchvision` from source?
  warn(

    Arguments received: ['yolo']. Ultralytics 'yolo' commands use the following syntax:

        yolo TASK MODE ARGS

        Where   TASK (optional) is one of ('detect', 'segment', 'classify', 'pose', 'obb')
                MODE (required) is one of ('train', 'val', 'predict', 'export', 'track', 'benchmark')
                ARGS (optional) are any number of custom 'arg=value' pairs like 'imgsz=320' that override defaults.
                    See all ARGS at https://docs.ultralytics.com/usage/cfg or with 'yolo cfg'

    1. Train a detection model for 10 epochs with an initial learning_rate of 0.01
        yolo train data=coco128.yaml model=yolov8n.pt epochs=10 lr0=0.01

    2. Predict a YouTube video using a pretrained segmentation model at image size 320:
        yolo predict model=yolov8n-seg.pt source='https://youtu.be/LNwODJXcvt4' imgsz=320

    3. Val a pretrained detection model at batch-size 1 and image size 640:
        yolo val model=yolov8n.pt data=coco128.yaml batch=1 imgsz=640

    4. Export a YOLOv8n classification model to ONNX format at image size 224 by 128 (no TASK required)
        yolo export model=yolov8n-cls.pt format=onnx imgsz=224,128

    5. Explore your datasets using semantic search and SQL with a simple GUI powered by Ultralytics Explorer API
        yolo explorer

    7. Run special commands:
        yolo help
        yolo checks
        yolo version
        yolo settings
        yolo copy-cfg
        yolo cfg

    Docs: https://docs.ultralytics.com
    Community: https://community.ultralytics.com
    GitHub: https://github.com/ultralytics/ultralytics

至此,环境配置完成。

配置 VSC

选择下载的 ultralytics 8.1.0 的文件夹。

配置终端和解释器。

下载 YOLO 模型

有警告:

(yolov8) D:\gesture_recog\ultralytics-8.1.0>yolo mode='predict' task='detect' model='yolov8m.pt' source='00001.jpeg'
C:\Users\Chi\miniconda3\envs\yolov8\lib\site-packages\torchvision\io\image.py:14: UserWarning: Failed to load image Python extension: '[WinError 127] 找不到指定的程序。'If you don't plan on using image functionality from `torchvision.io`, you can ignore this warning. Otherwise, there might be something wrong with your environment. Did you have `libjpeg` or `libpng` installed before building `torchvision` from source?
  warn(
D:\gesture_recog\ultralytics-8.1.0\ultralytics\nn\tasks.py:634: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
  return torch.load(file, map_location="cpu"), file  # load

image 1/1 D:\gesture_recog\ultralytics-8.1.0\00001.jpeg: 384x640 19 cars, 4 buss, 70.9ms
Speed: 7.6ms preprocess, 70.9ms inference, 148.1ms postprocess per image at shape (1, 3, 384, 640)
💡 Learn more at https://docs.ultralytics.com/modes/predict

之后重新安装了 TorchVision

(yolov8) D:\gesture_recog\ultralytics-8.1.0>conda install torchvision -c pytorch

完成之后测试:发现 Pillow 的 _imaging 模块加载失败

(yolov8) D:\gesture_recog\ultralytics-8.1.0>yolo mode='predict' task='detect' model='yolov8m.pt' source='00001.jpeg'
Traceback (most recent call last):
  File "C:\Users\Chi\miniconda3\envs\yolov8\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Chi\miniconda3\envs\yolov8\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\Chi\miniconda3\envs\yolov8\Scripts\yolo.exe\__main__.py", line 4, in <module>
  File "D:\gesture_recog\ultralytics-8.1.0\ultralytics\__init__.py", line 5, in <module>
    from ultralytics.data.explorer.explorer import Explorer
  File "D:\gesture_recog\ultralytics-8.1.0\ultralytics\data\__init__.py", line 3, in <module>
    from .base import BaseDataset
  File "D:\gesture_recog\ultralytics-8.1.0\ultralytics\data\base.py", line 17, in <module>
    from ultralytics.utils import DEFAULT_CFG, LOCAL_RANK, LOGGER, NUM_THREADS, TQDM
  File "D:\gesture_recog\ultralytics-8.1.0\ultralytics\utils\__init__.py", line 19, in <module>
    import matplotlib.pyplot as plt
  File "C:\Users\Chi\miniconda3\envs\yolov8\lib\site-packages\matplotlib\__init__.py", line 161, in <module>
    from . import _api, _version, cbook, _docstring, rcsetup
  File "C:\Users\Chi\miniconda3\envs\yolov8\lib\site-packages\matplotlib\rcsetup.py", line 28, in <module>
    from matplotlib.colors import Colormap, is_color_like
  File "C:\Users\Chi\miniconda3\envs\yolov8\lib\site-packages\matplotlib\colors.py", line 52, in <module>
    from PIL import Image
  File "C:\Users\Chi\miniconda3\envs\yolov8\lib\site-packages\PIL\Image.py", line 100, in <module>
    from . import _imaging as core
ImportError: DLL load failed while importing _imaging: 找不到指定的模块。

随后重装 pillow 测试还是不行

然后通过 conda 包管理器从 conda-forge 中安装 pillow 包。

conda install -c conda-forge pillow

好了。

from ultralytics import YOLO

yolo = YOLO(model='..\Models\yolov8m.pt', task='detect')

result=yolo(source=0, save=True)

认识 YOLO

ultralytics\cfg\default.yaml # 存放参数和一些默认参数形式

训练

查看Pytorch是否能检查到GPU

import torch
print(torch.cuda.is_available())
yolo task=detect mode=train model=..\Model\yolov8n.pt data=D:/gesture_recog/ASL/data.yaml epochs=50 imgsz=640 batch=4 device=0

# 验证模型
yolo task=detect mode=val model=runs/detect/train/weights/best.pt data=D:/gesture_recog/ASL/data.yaml

人脸识别测试:

(yolov8) D:\facial_recog\ultralytics-8.1.0>yolo task=detect mode=val model=runs/detect/train3/weights/best.pt data=D:\facial_recog\Faces\data.yaml     
(yolov8) D:\gesture_recog\ultralytics-8.1.0>yolo task=detect mode=val model=runs/detect/train/weights/best.pt data=D:/gesture_recog/ASL/data.yaml
D:\gesture_recog\ultralytics-8.1.0\ultralytics\nn\tasks.py:634: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
  return torch.load(file, map_location="cpu"), file  # load
Ultralytics YOLOv8.1.0 🚀 Python-3.10.16 torch-2.5.1 CUDA:0 (NVIDIA GeForce RTX 3050 Ti Laptop GPU, 4096MiB)
Model summary (fused): 168 layers, 3010718 parameters, 0 gradients, 8.1 GFLOPs
val: Scanning D:\gesture_recog\ASL\valid\labels.cache... 1320 images, 60 backgrounds, 0 corrupt: 100%|██████████| 1320/1320 [00:00<?, ?it/s]
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|██████████| 83/83 [00:13<00:00,  6.27it/s]
                   all       1320       1270      0.921      0.889      0.945      0.785
                     A       1320         41      0.989      0.927      0.963      0.812
                     B       1320         53      0.877          1      0.989      0.839
                     C       1320         51      0.944      0.922       0.98      0.777
                     D       1320         44      0.906      0.876      0.949      0.761
                     E       1320         46          1      0.909      0.949      0.862
                     F       1320         49      0.981      0.898      0.983      0.895
                     G       1320         51      0.843      0.841      0.897      0.761
                     H       1320         46      0.903      0.957      0.956      0.773
                     I       1320         48      0.721      0.729      0.782      0.536
                     J       1320         48      0.954      0.861      0.938      0.697
                     K       1320         51      0.899      0.804      0.893      0.726
                     L       1320         56          1      0.905      0.984      0.826
                     M       1320         50      0.934       0.92      0.982      0.845
                     N       1320         29      0.891       0.85      0.968       0.83
                     O       1320         54      0.958      0.836      0.947      0.701
                     P       1320         44      0.943      0.886      0.939      0.687
                     Q       1320         44      0.831      0.818      0.858      0.744
                     R       1320         52      0.909      0.957      0.974      0.875
                     S       1320         67      0.962      0.881      0.965      0.851
                     T       1320         45      0.926      0.911      0.959       0.89
                     U       1320         53      0.938      0.943      0.977      0.852
                     V       1320         48      0.937      0.929      0.961      0.798
                     W       1320         46      0.977      0.942      0.984      0.842
                     X       1320         57      0.981      0.895       0.96      0.723
                     Y       1320         55      0.928      0.938       0.97      0.802
                     Z       1320         42      0.824      0.786      0.867      0.716
Speed: 0.4ms preprocess, 4.3ms inference, 0.0ms loss, 1.3ms postprocess per image
Results saved to runs\detect\val2
💡 Learn more at https://docs.ultralytics.com/modes/val
# 使用模型预测
yolo task=detect mode=predict model=runs/detect/train/weights/best.pt source=path/to/images_or_video

Pasted image 20250318224527.png

安装 Flask 库:

(yolov8) D:\gesture_recog\Flask>pip install flask
ServerClientCameraClientServerClientCameraClientgetUserMedia()视频流发送视频帧返回处理结果本地渲染结果

视频采集模块

摄像头设备

用户授权

调用MediaDevices.getUserMedia

获取视频流

原始帧输出

YOLOv8n服务端客户端YOLOv8n服务端客户端发送视频帧(WebSocket)帧预处理(640x640)推理结果(26类手势)返回检测数据

数据传输模块

连接建立

二进制流

检测结果

客户端

发送视频帧

服务端接收

返回客户端

显示模块

接收检测结果

解析数据

渲染到界面

支持交互